package com.witframework.web.tag.menuwordtag;

import java.util.Vector;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * It is 
 * <m:MenuWord> 
 * <m:text>a special word</m:text> 
 * <m:link value="javascript:alert('Option 1')">Option 1</m:link>
 * <m:link value="/index.html">Option 2</m:link> 
 * </m:MenuWord> 
 * in your text
 * 
 * @author wuxinyang Aug 17, 2009 9:24:29 AM
 */
public class MenuWordTag extends BodyTagSupport {
	private static final String MENUWORDJS = "mnwrdcj2005";
	private static final String MENU = "menu";
	private static final String MENUWORD = "mnwrd";
	private static final String DEFMENU_STYLE = "background-color:white;";
	private static final String DEF_STYLE = "cursor:pointer; text-decoration:underline;";
	private Vector nodes = null;
	private String id = null;
	private String text = null;
	private String style = null;
	private String menuStyle = null;

	public void setId(String paramString) {
		this.id = paramString;
	}

	public String getId() {
		return this.id;
	}

	public String getText() {
		return this.text;
	}

	public void setText(String paramString) {
		this.text = paramString;
	}

	public void setStyle(String paramString) {
		this.style = paramString;
	}

	public String getStyle() {
		return this.style;
	}

	public void setMenuStyle(String paramString) {
		this.menuStyle = paramString;
	}

	public String getMenuStyle() {
		return this.menuStyle;
	}

	public void addLink(String paramString1, String paramString2, String paramString3, String paramString4) {
		LinkBean localLinkBean = new LinkBean();
		localLinkBean.setText(paramString2);
		if (paramString1 != null)
			localLinkBean.setValue(paramString1);
		if (paramString3 != null)
			localLinkBean.setStyle(paramString3);
		if (paramString4 != null)
			localLinkBean.setClassName(paramString4);
		if (this.nodes == null)
			this.nodes = new Vector();
		this.nodes.addElement(localLinkBean);
	}

	public int doAfterBody() throws JspException {
		BodyContent localBodyContent = getBodyContent();
		if (localBodyContent != null)
			localBodyContent.clearBody();
		return 0;
	}

	public int doEndTag() throws JspException {
		int ii;
		Integer localInteger;
		StringBuffer localStringBuffer = new StringBuffer("");
		if ((localInteger = (Integer) this.pageContext.getAttribute("mnwrdcj2005")) == null) {
			localStringBuffer.append(getCommonCode());
			localStringBuffer.append(getCSS());
			ii = 1;
		} else {
			ii = localInteger.intValue() + 1;
		}
		this.pageContext.setAttribute("mnwrdcj2005", new Integer(ii), 1);
		if (this.text == null)
			this.text = "";
		if (this.nodes == null) {
			try {
				this.pageContext.getOut().write(this.text);
			} catch (Exception localException1) {
				throw new JspException("Could not write data " + localException1.toString());
			}
			dropData();
			return 6;
		}
		if (this.nodes.size() == 0) {
			try {
				this.pageContext.getOut().write(this.text);
			} catch (Exception localException2) {
				throw new JspException("Could not write data " + localException2.toString());
			}
			dropData();
			return 6;
		}
		if (this.id == null)
			this.id = "mnwrd" + ii;
		String str = this.id + "menu";
		localStringBuffer.append("<div id=\"");
		localStringBuffer.append(str);
		localStringBuffer.append("\" class=\"menuword\"");
		if (this.menuStyle == null)
			this.menuStyle = "background-color:white;";
		localStringBuffer.append(" style=\"");
		localStringBuffer.append(this.menuStyle);
		localStringBuffer.append("\"");
		localStringBuffer.append(" onmouseover=\"showWordMenu(this)\" onmouseout=\"hideWordMenu(this)\">\n");
		localStringBuffer.append("<ul>\n");
		for (int i = 0; i < this.nodes.size(); ++i) {
			LinkBean localLinkBean = (LinkBean) this.nodes.elementAt(i);
			localStringBuffer.append("<li value=\"");
			localStringBuffer.append(i);
			localStringBuffer.append("\">");
			if (localLinkBean.getClassName() != null) {
				localStringBuffer.append("<div class=\"");
				localStringBuffer.append(localLinkBean.getClassName());
				localStringBuffer.append("\">");
			}
			localStringBuffer.append("<a");
			if (localLinkBean.getStyle() != null) {
				localStringBuffer.append(" style=\"");
				localStringBuffer.append(localLinkBean.getStyle());
				localStringBuffer.append("\"");
			}
			localStringBuffer.append(" href=\"");
			localStringBuffer.append(localLinkBean.getValue());
			localStringBuffer.append("\">");
			localStringBuffer.append(localLinkBean.getText());
			localStringBuffer.append("</a>");
			if (localLinkBean.getClassName() != null)
				localStringBuffer.append("</div>");
			localStringBuffer.append("</li>\n");
		}
		localStringBuffer.append("</ul></div>\n");
		localStringBuffer.append("\n<span id=\"");
		localStringBuffer.append(this.id);
		localStringBuffer.append("\"");
		if (this.style == null)
			this.style = "cursor:pointer; text-decoration:underline;";
		localStringBuffer.append(" style=\"");
		localStringBuffer.append(this.style);
		localStringBuffer.append("\"");
		localStringBuffer.append(" onmouseover=\"popupWordMenu(this,'");
		localStringBuffer.append(str);
		localStringBuffer.append("');\"");
		localStringBuffer.append(" onmouseout=\"hideWordMenu(getWordElem('");
		localStringBuffer.append(str);
		localStringBuffer.append("'));\"");
		localStringBuffer.append(">");
		localStringBuffer.append(this.text);
		localStringBuffer.append("</span>\n");
		try {
			this.pageContext.getOut().write(localStringBuffer.toString());
		} catch (Exception localException3) {
			throw new JspException("Could not write data " + localException3.toString());
		}
		dropData();
		return 6;
	}

	public void release() {
		super.release();
		dropData();
	}

	private void dropData() {
		this.nodes = null;
		this.style = null;
		this.menuStyle = null;
		this.id = null;
		this.text = null;
	}

	private String getCommonCode() {
		StringBuffer localStringBuffer = new StringBuffer("");
		localStringBuffer.append("\n<script language=\"JavaScript\">\n");
		localStringBuffer
				.append("function getWordX(o) { var x=0; if(document.layers) x=o.pageX; else { while(eval(o)) { x+=o.offsetLeft; o=o.offsetParent; } } return x; };\n");
		localStringBuffer
				.append("function getWordY(o) { var y=0; if(document.layers) y=o.pageY; else { while(eval(o)) { y+=o.offsetTop; o=o.offsetParent; } } return y; };\n");
		localStringBuffer.append("function getWordElem(id){\n");
		localStringBuffer.append("if (document.getElementById) return document.getElementById(id);\n");
		localStringBuffer.append("else if (document.layers) return document.layers[id];\n");
		localStringBuffer.append("else if (document.all) return document.all[id];\n");
		localStringBuffer.append("}\n");
		localStringBuffer.append("function hideWordMenu(obj){\n");
		localStringBuffer.append("if (document.layers) obj.visibility='hide';\n");
		localStringBuffer.append("else obj.style.visibility='hidden';\n");
		localStringBuffer.append("}\n");
		localStringBuffer.append("function showWordMenu(obj){\n");
		localStringBuffer.append("if (document.layers) obj.visibility='show';\n");
		localStringBuffer.append("else obj.style.visibility='visible';\n");
		localStringBuffer.append("}\n");
		localStringBuffer.append("function popupWordMenu(objButton, idMenu){\n");
		localStringBuffer.append("o=getWordElem(idMenu);\n");
		localStringBuffer.append("var nTop = getWordY(objButton);\n");
		localStringBuffer.append("if (objButton.offsetHeight) nTop+=objButton.offsetHeight;\n");
		localStringBuffer.append("var nLeft = getWordX(objButton);\n");
		localStringBuffer.append("if (document.layers)\n");
		localStringBuffer.append("{ o.pageY=nTop+'px';\n");
		localStringBuffer.append("  o.pageX=nLeft+'px';}\n");
		localStringBuffer.append("else\n");
		localStringBuffer.append("{ o.style.top=nTop+'px';\n");
		localStringBuffer.append("  o.style.left = nLeft+'px'; }\n");
		localStringBuffer.append("showWordMenu(o);\n");
		localStringBuffer.append("}\n");
		localStringBuffer.append("</script>\n");
		return localStringBuffer.toString();
	}

	private String getCSS() {
		StringBuffer localStringBuffer = new StringBuffer("");
		localStringBuffer.append("\n<style>\n");
		localStringBuffer
				.append(".menuword {top:0;left:0;position:absolute;z-index:2;visibility:hidden;border-style:solid;border-width:1px;font:75% verdana;}\n");
		localStringBuffer.append(".menuword ul{margin:0;list-style:none;padding:.5em;}\n");
		localStringBuffer.append(".menuword li{cursor:pointer;cursor:hand;}\n");
		localStringBuffer.append(".menuword a, a:visited{text-decoration:none;}\n");
		localStringBuffer.append(".menuword a:hover{ text-decoration:underline;}\n");
		localStringBuffer.append("</style>\n");
		return localStringBuffer.toString();
	}
}
